package com.qiaomeng.flutter.flutter_jpush_vip;

import android.app.Activity;
import android.content.Intent;

import androidx.annotation.NonNull;

import java.util.HashMap;
import java.util.Map;

import cn.jpush.android.ups.TokenResult;
import cn.jpush.android.ups.UPSRegisterCallBack;
import io.flutter.embedding.engine.plugins.FlutterPlugin;
import io.flutter.embedding.engine.plugins.activity.ActivityAware;
import io.flutter.embedding.engine.plugins.activity.ActivityPluginBinding;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;
import io.flutter.plugin.common.MethodChannel.MethodCallHandler;
import io.flutter.plugin.common.MethodChannel.Result;
import io.flutter.plugin.common.PluginRegistry.Registrar;

/**
 * FlutterJpushVipPlugin
 */
public class FlutterJpushVipPlugin implements FlutterPlugin, MethodCallHandler {
    /// The MethodChannel that will the communication between Flutter and native Android
    ///
    /// This local reference serves to register the plugin with the Flutter Engine and unregister it
    /// when the Flutter Engine is detached from the Activity
    private static MethodChannel channel;
    private static Handler handler;
    private static String message;
    private static boolean isInit = false;

    @Override
    public void onAttachedToEngine(@NonNull FlutterPluginBinding flutterPluginBinding) {
        channel = new MethodChannel(flutterPluginBinding.getFlutterEngine().getDartExecutor(), "flutter_jpush_vip");
        channel.setMethodCallHandler(this);
        handler = new Handler(flutterPluginBinding.getApplicationContext());
    }

    public static void registerWith(Registrar registrar) {
        final MethodChannel channel = new MethodChannel(registrar.messenger(), "flutter_jpush_vip");
        channel.setMethodCallHandler(new FlutterJpushVipPlugin());
    }

    @Override
    public void onMethodCall(@NonNull MethodCall call, @NonNull final Result result) {
        if (call.method.equals("init")) {
            isInit = true;
            onNewIntent(null);
            handler.init(new UPSRegisterCallBack() {
                @Override
                public void onResult(TokenResult tokenResult) {
                    Map map = new HashMap();
                    map.put("registrationId", tokenResult.getToken());
                    map.put("code", tokenResult.getReturnCode());
                    map.put("actionType", tokenResult.getActionType());
                    result.success(map);
                }
            });
        } else if (call.method.equals("debug")) {
            handler.debug();
            result.notImplemented();
        } else if (call.method.equals("getRegistrationID")) {
            String id = handler.getRegistrationID();
            result.success(id);
        } else if (call.method.equals("clearAllNotifications")) {
            handler.clearAllNotifications();
            result.notImplemented();
        } else {
            result.notImplemented();
        }
    }

    @Override
    public void onDetachedFromEngine(@NonNull FlutterPluginBinding binding) {
        channel.setMethodCallHandler(null);
    }

    /**
     * 监听消息渠道
     *
     * @param intent
     */
    public static void onNewIntent(Intent intent) {
        if (isInit && message != null) {
            channel.invokeMethod("__JPUSH_MESSAGE__", message);
            message = null;
            return;
        }
        if (intent == null) {
            return;
        }
        String msg = handler.getPushMessage(intent);
        if (msg == null) {
            return;
        }
        if (isInit) {
            channel.invokeMethod("__JPUSH_MESSAGE__", msg);
        } else {
            message = msg;

        }
    }
}
